// 数组的浅拷贝


const student = { name: '王同学', age: { num: 20 } };
 
// 浅拷贝
        const newStudent1 = { ...student } //说明扩展运算符只能浅拷贝
 
       student.name = '王同学',
 
       student.age.num = 30
 
        console.log(newStudent)  //输出结果为：name：“王同学”，num：30



        const student = { name: '王同学', age: { num: 20 } }
 
        const newStudent2 = Object.assign({},student); //只改变了外层地址，所以依然是浅拷贝
 
       student.name = '李四';
 
       student.age.num = 30
 
       console.log(newStudent);


       // 深拷贝
       const obj = {a:1,b:2,c:{d:3}}
       function deepClone(data){
 
         if(typeof data == 'object'){
 
  //判断拷贝的要进行深拷贝的是数组还是对象，是数组的话进行数组拷贝，对象的话进行对象拷贝
 
         const newData = {}
           for(const key in data){
   
             newData[key] = deepClone(data[key]);
   
          }
           return newData;
       }
         return data
   }
      const newObj = deepClone(obj)
    
      obj.c.d = 4;
     
      console.log(newObj);//输出d：3



      const student = { name: '王同学', age: { num: 20 } }
  
      const newStudent = JSON.parse(JSON.stringify(student)); //外层和内层地址都发生改变，所以是深拷贝
            student.age.num = 30;
            console.log(newStudent);  //num依然是20



      //如何来求两个数组的交集
      let s1 = new Set(a1)
      let s2 = new Set(a2)
      let jjarr = []
      arr1.forEach(value=>{
        if(s2.has(value))
            jjarr.push(value)
      })
      console.log([...new Set(jjarr)])
      console.log(Array.from(new Set(jjarr)))
      //如何求两个数组的差集
      let res = []
      arr1.forEach(value=>{
        if(!s2.has(value))
            res.push(value)
      })
      arr2.forEach(value=>{
        if(!s1.has(value))
            res.push(value)
      })
      console.log(res)
      console.log([...new Set(res)])          


